$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sjdriver/sjFinancelist',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
            { label: '公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 50 ,hidden:true},
			{ label: '分公司名称', name: 'branchCompany', index: 'branch_company', width: 120 },
			{ label: '网点id', name: 'netPointId', index: 'net_point_id', width: 50 ,hidden:true},
			{ label: '网点名称', name: 'netPoint', index: 'net_point', width: 120 },
			{ label: '用户名', name: 'username', index: 'username', width: 80 },
            { label: '真实姓名', name: 'realname', index: 'realname', width: 80 },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 ,hidden:true},
            { label: '性别', name: 'sex', index: 'sex', width: 30 ,
                formatter:function(v){
                    if(v==0){
                        return "女";
                    }else if(v==1){
                        return "男";
                    }else{
                        return "";
                    }
                }
            },
            { label: '年龄', name: 'age', index: 'age', width: 30 },
            { label: '车型', name: 'carstyle', index: 'carstyle', width: 80 },
            { label: '车牌', name: 'carno', index: 'carno', width: 80 },
            { label: '注册时间', name: 'createTime', index: 'create_time', width: 100 },
            { label: '总订单数', name: 'completeOrderSum', index: 'complete_order_sum', width: 50 },
            { label: '赚取金额(元)', name: 'balance', index: 'balance', width: 80 },
			{ label: '操作', width: 80 ,formatter: function(value, options, row){
			 return '<input type="button" class="btn btn-primary" onclick="vm.showDetail('+row.id+')" value="财务详情"/>';
			 }}

        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.getBranchCompanyList();
});

var vm = new Vue({
	el:'#rrapp',
    data:{
        q:{
            branchCompany:null,
            netPoint:null,
            id:null
        },
        showList: true,
        title: null,
        sjDriver: {
            branchCompany:null,
            branchCompanyId:null,
            netPoint:null,
            netPointId:null,
            realname:null
        }
    },
	methods: {
		query: function () {
			vm.reload();
		},

        empty: function () {
            vm.sjDriver.branchCompany = null;
            vm.sjDriver.branchCompanyId = null;
            vm.sjDriver.netPoint = null;
            vm.sjDriver.netPointId = null;
            vm.sjDriver.realname = null;
            vm.q.branchCompany = null;
            vm.q.netPoint = null;
            vm.q.realname = null;
            vm.reload();
        },

		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'branchCompany': vm.q.branchCompany,'netPoint':vm.q.netPoint,'realname':vm.sjDriver.realname},
                page:page
            }).trigger("reloadGrid");
		},


		showDetail: function (id) {
		    vm.q.id = id;
            vm.showList = false;
            $("#jqGrid2").jqGrid({
                url: baseURL + 'psdriverstatistics/detailList',
                datatype: "json",
                postData:{'driverId': id},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 110, key: true,hidden:true },
                    { label: '司机id', name: 'driverId', index: 'driver_id', width: 110,hidden:true },
                    { label: '司机姓名', name: 'driverName', index: 'driver_name', width: 140 },
                    { label: '车型', name: 'carStyle', index: 'car_style', width: 150 },
                    { label: '支付宝收入', name: 'aliIncome', index: 'ali_income', width: 140 },
                    { label: '微信收入', name: 'wxIncome', index: 'wx_income', width: 140 },
                    { label: '现金收入', name: 'caseIncome', index: 'case_income', width: 140 },
                    { label: '总收入', name: 'sumIncome', index: 'sum_income', width: 140 },
                    { label: '平台收入', name: 'platformIncome', index: 'platform_income', width: 140 },
                    { label: '扣除', name: 'deduction', index: 'deduction', width: 140 },
                    { label: '已提现', name: 'withdrawed', index: 'withdrawed', width: 140 },
                    { label: '可提现', name: 'withdrawable', index: 'withdrawable', width: 140 },
                    { label: '账户余额', name: 'balance', index: 'balance', width: 130 }
                ],
                viewrecords: true,
                height: 40,
                rowNum: 2,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                shrinkToFit:false,
                autoScroll: true,
                pager: "#jqGridPager2",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGrid2").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });


            $("#jqGrid3").jqGrid({
                url: baseURL + 'psorder/list',
                datatype: "json",
                postData:{'driverId': id,'orderStatus':2},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
                    { label: '订单号', name: 'ordernum', index: 'ordernum', width: 120 ,hidden:true},
                    { label: '用户id', name: 'userId', index: 'user_id', width: 80 ,hidden:true},
                    { label: '用户姓名', name: 'userName', index: 'user_name', width: 80 ,hidden:true},
                    { label: '用户电话', name: 'userPhone', index: 'user_phone', width: 80 ,hidden:true},
                    { label: '接单司机id', name: 'driverId', index: 'driver_id', width: 80 ,hidden:true},
                    { label: '司机姓名', name: 'driverName', index: 'driver_name', width: 120 },
                    { label: '接单司机电话', name: 'driverTel', index: 'driver_tel', width: 120 },
                    { label: '下单时间', name: 'createTime', index: 'create_time', width: 120 ,hidden:true},
                    { label: '接收订单时间', name: 'receiveTime', index: 'receive_time', width: 80 ,hidden:true},
                    { label: '完成订单时间', name: 'completeTime', index: 'complete_time', width: 120 },
                    { label: '起点名称', name: 'startName', index: 'start_name', width: 210 },
                    { label: '起点经度', name: 'startLng', index: 'start_lng', width: 80 ,hidden:true},
                    { label: '起点纬度', name: 'startLat', index: 'start_lat', width: 80 ,hidden:true},
                    { label: '终点名称', name: 'endName', index: 'end_name', width: 210 },
                    { label: '终点经度', name: 'endLng', index: 'end_lng', width: 80,hidden:true },
                    { label: '终点纬度', name: 'endLat', index: 'end_lat', width: 80,hidden:true },
                    { label: '当前地址名称', name: 'curAddress', index: 'cur_address', width: 80,hidden:true },
                    { label: '当前经度', name: 'curLng', index: 'cur_lng', width: 80 ,hidden:true},
                    { label: '当前纬度', name: 'curLat', index: 'cur_lat', width: 80 ,hidden:true},
                    { label: '行车距离(理论)', name: 'distance', index: 'distance', width: 80 ,hidden:true},
                    { label: '实际行驶距离', name: 'realdistance', index: 'realdistance', width: 100 },
                    { label: '备注', name: 'note', index: 'note', width: 80 ,hidden:true},
                    { label: '订单状态', name: 'orderStatus', index: 'order_status', width: 80,
                        formatter:function(v){
                            if(v==0){
                                return "未完成";
                            }else if(v==1){
                                return "已完成";
                            }else if(v==2){
                                return "已支付";
                            }else if(v==3){
                                return "已取消";
                            }else{
                                return "";
                            }
                        }
                    },
                    { label: '订单金额', name: 'orderMoney', index: 'order_money', width: 100 },
                    { label: '支付方式', name: 'payType', index: 'pay_type', width: 100 ,
                        formatter:function(v){
                            if(v=="wxpay"){
                                return "微信支付";
                            }else if(v=="alipay"){
                                return "支付宝支付";
                            }else if(v=="cash"){
                                return "现金";
                            }else{
                                return "";
                            }
                        }
                    },
                    { label: '支付时间', name: 'payTime', index: 'pay_time', width: 120 },
                    { label: '实际支付金额', name: 'payRealymoney', index: 'pay_realymoney', width: 100 },
                    { label: '额外费用', name: 'extraCharge', index: 'extra_charge', width: 100 },
                    { label: '额外费用说明', name: 'extraChargeNote', index: 'extra_charge_note', width: 80 ,hidden:true},
                    { label: '支付返回的订单号', name: 'payOrder', index: 'pay_order', width: 80 ,hidden:true},
                    { label: '支付状态', name: 'payStatus', index: 'pay_status', width: 80 ,
                        formatter:function(v){
                            if(v==0){
                                return "未支付";
                            }else if(v==1){
                                return "已支付";
                            }else{
                                return "";
                            }
                        }
                    }
                ],
                viewrecords: true,
                height: 500,
                rowNum: 20,
                rowList : [20,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                shrinkToFit:true,
                autoScroll: true,
                pager: "#jqGridPager3",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                   /* $("#jqGrid3").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });*/
                }
            });
		},

        excel: function() {
            window.location.href= baseURL + "psorder/createExcel";
        },

        backsj: function () {
            window.location.href ="sjmessage.html";
        },

        exportExcel: function () {
            alert('导出excel');
            window.location.href= baseURL + "psorder/list/export?op=export";
        },

        BranchCompanySelect:function(){
            utils.openDialogView('分公司列表',$("#windowBranchCompany"),'900px','500px',1);
        },
        getBranchCompanyList: function(){
            $("#jqGridBranchCompany").jqGrid({
                url: baseURL + 'psbranchcompany/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
                    { label: '分公司名称', name: 'companyName', index: 'company_name', width: 100 },
                    { label: '分公司营业执照', name: 'companyLicenceImg', index: 'company_licence_img', width: 80 ,hidden:true},
                    { label: '分公司地址', name: 'address', index: 'address', width: 150 },
                    { label: '联系电话', name: 'contactNumber', index: 'contact_number', width: 80 },
                    { label: '联系人', name: 'contactPeople', index: 'contact_people', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 85,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerBranchCompany",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridBranchCompany").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        BranchPointSelect:function(){
            if( vm.sjDriver.branchCompanyId == null ) {
                alert("请先选择一个分公司!");
                return ;
            }
            utils.openDialogView('网点列表',$("#windowNetPoint"),'900px','500px',1);
        },
        getjqGridNetPoint: function(branchCompanyId){
            $("#jqGridNetPoint").jqGrid({
                url: baseURL + 'psnetpoint/list',
                datatype: "json",
                postData:{'branchCompanyId': branchCompanyId},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true},
                    { label: '网点名称', name: 'pointName', index: 'point_name', width: 120 },
                    { label: '负责人', name: 'person', index: 'person', width: 120 },
                    { label: '电话', name: 'telphone', index: 'telphone', width: 80 },
                    { label: '所属分公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 80 ,hidden:true},
                    { label: '所属分公司名称', name: 'branchCompany', index: 'branch_company', width: 130 ,hidden:true},
                    { label: '网点地址', name: 'address', index: 'address', width: 300 },
                    { label: '营业时间', name: 'shopHours', index: 'shop_hours', width: 80 ,hidden:true},
                    { label: '收件时间', name: 'receivingTime', index: 'receiving_time', width: 80 ,hidden:true},
                    { label: '经度', name: 'longitude', index: 'longitude', width: 80 ,hidden:true},
                    { label: '纬度', name: 'latitude', index: 'latitude', width: 80 ,hidden:true},
                    { label: '省', name: 'province', index: 'province', width: 80,hidden:true },
                    { label: '市', name: 'city', index: 'city', width: 80 ,hidden:true},
                    { label: '区', name: 'country', index: 'country', width: 80 ,hidden:true},
                    { label: '高德云图id(高德检索用)', name: 'gaoId', index: 'gao_id', width: 80 ,hidden:true}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 25,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerNetPoint",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridNetPoint").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        }

	}
});


/** 分公司弹窗确定和关闭按钮处理 **/
$("#addBranchCompany").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridBranchCompany").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择分公司!");
        return ;
    }
    var rowData = $("#jqGridBranchCompany").jqGrid('getRowData',id);
    vm.sjDriver.branchCompany = rowData.companyName;
    vm.sjDriver.branchCompanyId = rowData.id;
    vm.q.branchCompany = rowData.companyName;

    utils.closeCurrentPage();
    vm.getjqGridNetPoint(vm.sjDriver.branchCompanyId);
});

$("#closeBranchCompany").click(function(){
    utils.closeCurrentPage();
});

/** 网点弹窗口确定和关闭按钮处理 **/
$("#addNetPoint").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridNetPoint").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择网点!");
        return ;
    }
    var rowData = $("#jqGridNetPoint").jqGrid('getRowData',id);
    vm.sjDriver.netPoint = rowData.pointName;
    vm.sjDriver.netPointId = rowData.id;
    vm.q.netPoint = rowData.pointName;

    utils.closeCurrentPage();
});

$("#closeNetPoint").click(function(){
    utils.closeCurrentPage();
});